package com.simple.gateway.assist.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.simple.gateway.assist.common.Result;
import com.simple.gateway.assist.exception.GatewayException;
import com.simple.gateway.assist.service.bo.ApplicationSystemRichBO;
import com.simple.gateway.assist.service.param.RegisterGatewayParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 项目: simple-gateway
 * <p>
 * 功能描述: 网关注册服务
 *
 * @author: WuChengXing
 * @create: 2023-08-07 21:39
 **/
public class RegisterGatewayService {

    private final Logger logger = LoggerFactory.getLogger(RegisterGatewayService.class);

    public void doRegister(String address, RegisterGatewayParam param) {
        String resultStr = "";
        try {
            resultStr = HttpUtil.post(address + "/gateway/api/config/registerGateway", JSON.toJSONString(param), 3000);
        } catch (Exception e) {
            logger.error("网关服务注册异常，链接资源不可用：{}", address + "/gateway/api/config/registerGateway");
            throw e;
        }

        Result result = JSON.parseObject(resultStr, Result.class);
        logger.info("向网关中心注册网关算力服务：{} 注册结果：{}", JSON.toJSONString(param), resultStr);
        if (!"0000".equals(result.getCode())) {
            throw new GatewayException("网关服务注册异常 [gatewayId：" + param.getGatewayId()
                    + "] 、[gatewayAddress：" + param.getGatewayAddress() + "]");
        }
    }

    public ApplicationSystemRichBO pullApplicationSystemRichInfo(String address, String gatewayId, String systemId) {
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("gatewayId", gatewayId);
        paramMap.put("systemId", systemId);
        String resultStr = "";
        try {
            resultStr = HttpUtil.post(address + "/gateway/api/config/queryApplicationSystemRichInfo", paramMap, 3000);
        } catch (Exception e) {
            logger.error("网关服务拉取异常，链接资源不可用：{}", address + "/gateway/api/config/queryApplicationSystemRichInfo");
            throw e;
        }

        Result<ApplicationSystemRichBO> result = JSON.parseObject(resultStr, new TypeReference<Result<ApplicationSystemRichBO>>() {
        });
        logger.info("从网关中心拉取应用服务和接口的配置信息到本地完成注册。gatewayId：{}", gatewayId);
        if (!"0000".equals(result.getCode())) {
            logger.error("从网关中心拉取应用服务和接口的配置信息到本地完成注册异常 [gatewayId：" + gatewayId + "]");
        }
        return result.getData();
    }

    public Map<String, String> queryRedisConfig(String address) {
        String resultStr;
        try {
            resultStr = HttpUtil.post(address + "/gateway/api/config/queryRedisConfig", "", 1550);
        } catch (Exception e) {
            logger.error("网关服务拉取配置异常，链接资源不可用：{}", address + "/gateway/api/config/queryRedisConfig", e);
            throw e;
        }
        Result<Map<String, String>> result = JSON.parseObject(resultStr, new TypeReference<Result<Map<String, String>>>() {
        });
        logger.info("从网关中心拉取Redis配置信息完成。result：{}", resultStr);
        if (!"0000".equals(result.getCode())) {
            throw new GatewayException("从网关中心拉取Redis配置信息异常");
        }
        return result.getData();
    }
}
